package top.fengleifeng.question;

/**
 * @author : fengelfeing
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年10月29日8:28 下午
 */
public class Num921使括号有效的最少添加 {

    class Solution {
        public int minAddToMakeValid(String S) {
            char[] chars = S.toCharArray();
            int leftHave = 0;
            int rightHave = 0;
            int count = 0;
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == '(') {
                    leftHave++;
                } else if (chars[i] == ')') {
                    if (leftHave <= 0) {
                        count++;
                    }else{
                        leftHave--;
                    }
                }
            }
            return count + leftHave;
        }
    }

    public static void main(String[] args) {
        String s = "())";
//        String s = "()))((";
        int i = new Num921使括号有效的最少添加().new Solution().minAddToMakeValid(s);
        System.out.println(i);
    }
}
